package com.cocoiko.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;

import com.cocoiko.utils.CocoikoAccount;
import com.cocoiko.utils.CocoikoAccountStore;
import com.cocoiko.utils.CocoikoComment;
import com.cocoiko.utils.CocoikoConstains;
import com.cocoiko.utils.CocoikoEvent;
import com.cocoiko.utils.CocoikoEventPostData;
import com.cocoiko.utils.CocoikoPoint;
import com.cocoiko.utils.CocoikoUser;
import com.cocoiko.utils.CocoikoUtils;

public class ClientUtils {

	/**
	 * 
	 * @param response
	 * @return data
	 * @throws IOException
	 */
	public static String readDataFromHttpResponse(HttpResponse response)
			throws IOException {

		BufferedReader reader = new BufferedReader(new InputStreamReader(
				response.getEntity().getContent()));

		StringBuffer sb = new StringBuffer("");
		String line = "";
		String NL = System.getProperty("line.separator");

		while ((line = reader.readLine()) != null) {
			sb.append(line + NL);
		}
		reader.close();

		return sb.toString();
	}

	/**
	 * execute post request
	 * 
	 * @param token
	 *            : if login token = null
	 * @param data
	 * @return
	 */
	public static String requestPostExecute(String url,
			List<NameValuePair> data, String token) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		if (token != null) {
			post.addHeader("Token", token);
		}
		try {
			post.setEntity(new UrlEncodedFormEntity(data));

			// response
			HttpResponse response = client.execute(post);

			// receive data
			return readDataFromHttpResponse(response);

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * execute get request
	 * 
	 * @param url
	 * @return response data
	 */
	public static String requestGetExecute(String url, String token) {

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet();
		if (token != null) {
			request.addHeader("Token", token);
		}
		try {
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);

			// receive data
			return readDataFromHttpResponse(response);

		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	/*********************************************************************
	 * 
	 * auth
	 * 
	 *********************************************************************/
	/**
	 * login
	 * 
	 * @param email
	 * @param pass
	 * @return account info
	 */
	public static String login(Context context, String email, String pass) {

		String url = CocoikoConstains.BASE_URL + "auth/login";

		// prepare post data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", pass));

		// login execute
		String data = requestPostExecute(url, params, null);

		if (data == null) {// can not connect to server

			return "can not connect to server";
		} else { //
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {// login success
					CocoikoAccount account = new CocoikoAccount();
					account.setId(String.valueOf(json.getInt("id")));
					account.setToken(json.getString("token"));
					account.setName(json.getString("name"));
					account.setGender(json.getString("gender"));
					account.setBirthday(json.getString("birthday"));
					account.setEmail(json.getString("email"));
					account.setIntroduce(json.getString("self_introduction"));

					int notification = json.getInt("notification");
					int mail = json.getInt("mail");

					boolean facebook = json.getBoolean("facebook");
					boolean twitter = json.getBoolean("twitter");
					if (facebook) {
						JSONObject facebook_id = json
								.getJSONObject("facebook_id");
					}
					if (twitter) {
						JSONObject twitter_id = json
								.getJSONObject("twitter_id");
					}

					CocoikoAccountStore.save(account, context);

					return null;
				} else {// login fail
					String message = json.getString("message");
					return message;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "login error";
			}

		}
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static boolean logout(Context context) {
		boolean status;

		String url = CocoikoConstains.BASE_URL + "auth/logout";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		if (data == null) {

			return false;
		} else {
			try {
				JSONObject json = new JSONObject(data);
				status = json.getBoolean("status");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return status;
	}

	public static String register(Context context, CocoikoAccount account) {
		String url = CocoikoConstains.BASE_URL + "auth/register";

		// prepare post data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", account.getEmail()));
		params.add(new BasicNameValuePair("password", CocoikoUtils.md5(account
				.getPassword())));
		params.add(new BasicNameValuePair("birthday", account.getBirthday()));
		params.add(new BasicNameValuePair("nickname", account.getName()));
		params.add(new BasicNameValuePair("gender", String.valueOf(account
				.getGenderInt())));
		// params.add(new BasicNameValuePair("seft_introduction", account
		// .getIntroduce()));

		String data = requestPostExecute(url, params, null);
		Log.i("Message", data);
		if (data == null) {
			return "can not connect to server ...";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					String id = String.valueOf(json.getInt("id"));
					String token = json.getString("token");
					account.setId(id);
					account.setToken(token);
					CocoikoAccountStore.save(account, context);
					return null;
				} else {
					String message = json.getString("message");
					return message;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}

		}
	}

	/**
	 * 
	 * @param context
	 * @param account
	 * @return
	 */
	public static String update(Context context, CocoikoAccount account) {
		String url = CocoikoConstains.BASE_URL + "auth/update";

		// prepare post data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", account.getEmail()));
		params.add(new BasicNameValuePair("password", CocoikoUtils.md5(account
				.getPassword())));
		params.add(new BasicNameValuePair("birthday", account.getBirthday()));
		params.add(new BasicNameValuePair("nickname", account.getName()));
		params.add(new BasicNameValuePair("gender", String.valueOf(account
				.getGenderInt())));
		params.add(new BasicNameValuePair("seft_introduction", account
				.getIntroduce()));

		String data = requestPostExecute(url, params, null);
		Log.i("Message", data);
		if (data == null) {
			return "can not connect to server ...";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					String id = String.valueOf(json.getInt("id"));
					String token = json.getString("token");
					CocoikoAccountStore.saveToken(context, id, token);
					return null;
				} else {
					String message = json.getString("message");
					return message;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}

		}
	}

	/*******************************************************************
	 * cocoiko home
	 *******************************************************************/

	/**
	 * 
	 */
	public static String cocoikoSync(Context context, CocoikoEvent event) {

		String url = CocoikoConstains.BASE_URL + "kokoiko/sync";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);

		// Log.i("token",token);
		// Log.i("sync data", data);
		if (data == null) {

			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);

				boolean status = json.getBoolean("status");
				if (status) {
					event.kid = json.getInt("kid");
					event.status = json.getString("kokoiko");
					event.date = new Date(json.getString("date"));

					JSONArray tenant = json.getJSONArray("tenant");
					JSONObject latest_tenant = tenant.getJSONObject(0);
					event.tenant.tid = latest_tenant.getInt("tid");
					event.tenant.address = latest_tenant.getString("address");
					event.tenant.name = latest_tenant.getString("name");
					event.tenant.genre = latest_tenant.getInt("genre");
					event.tenant.lat = (float) latest_tenant.getDouble("lat");
					event.tenant.lng = (float) latest_tenant.getDouble("lng");

					return null;
				} else {
					String message = json.getString("message");
					return message;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param event
	 * @return
	 */
	public static String cocoikoPost(Context context, CocoikoEventPostData event) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/post";

		// prepare post data
		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, event.getParams(), token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static String cocoikoTookme(Context context) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/tookme";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {

					// kokoiko hairetu shyori

					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param kid
	 * @param uid
	 * @return
	 */
	public static String cocoikoInfo(Context context, int kid, int uid) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/info";

		// prepare post data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray takes = json.getJSONArray("take");
					boolean taken = json.getBoolean("taken");
					JSONArray comments = json.getJSONArray("comments");

					// process

					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param kid
	 * @param uid
	 * @return
	 */
	public static String cocoikoTakeme(Context context, int kid, int uid) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/takeme";

		// prepare post data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param kid
	 * @return
	 */
	public static String cocoikoUntakeme(Context context, int kid) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/untakeme";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param kid
	 *            notnull
	 * @param message
	 *            not null
	 * @param limit
	 * @return
	 */
	public static String cocoikoUpdate(Context context, int kid,
			String message, int limit) {
		String url = CocoikoConstains.BASE_URL + "kokoiko/update";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));
		params.add(new BasicNameValuePair("message", message));
		if (limit != 0) {
			params.add(new BasicNameValuePair("limit", String.valueOf(limit)));
		}

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/*********************************************************************
	 * 
	 * comments
	 * 
	 *********************************************************************/
	/**
	 * 
	 */
	public static String commentAdd(Context context, int kid, String comment) {
		String url = CocoikoConstains.BASE_URL + "comments/add";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));
		params.add(new BasicNameValuePair("comment", comment));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					// int id = json.getInt("id");
					// id shyori
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param id
	 * @return
	 */
	public static String commentDelete(Context context, int id) {
		String url = CocoikoConstains.BASE_URL + "comments/delete";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", String.valueOf(id)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param kid
	 * @param comments
	 * @return
	 */
	public static String commentGet(Context context, int kid,
			ArrayList<CocoikoComment> comments) {
		String url = CocoikoConstains.BASE_URL + "comments/get";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("kid", String.valueOf(kid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray jsoncomments = json.getJSONArray("comments");
					for (int i = 0; i < jsoncomments.length(); i++) {
						CocoikoComment comment = new CocoikoComment();
						JSONObject object = jsoncomments.getJSONObject(i);
						comment.nickname = object.getString("nickname");
						comment.gender = object.getInt("gender");
						comment.kid = object.getInt("kid");
						comment.uid = object.getInt("uid");
						comment.comment = object.getString("comment");

						comments.add(comment);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**********************************************************************
	 * 
	 * search
	 * 
	 **********************************************************************/

	/**
	 * 
	 */
	public static String toJsonString(String[] data) {
		String result = "[";
		for (int i = 0; i < data.length - 1; i++) {
			result += data[i] + ",";
		}
		result += data[data.length - 1] + "]";
		return result;
	}

	/**
	 * 
	 * @param context
	 * @param email
	 * @return
	 */
	public static String searchContacts(Context context, String[] email,
			ArrayList<CocoikoUser> users) {
		String url = CocoikoConstains.BASE_URL + "search/contacts";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", toJsonString(email)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("users");
					for (int i = 0; i < array.length(); i++) {
						CocoikoUser user = new CocoikoUser();
						JSONObject object = array.getJSONObject(i);
						user.id = object.getInt("id");
						user.nickname = object.getString("nickname");
						user.is_followed = object.getBoolean("is_followed");

						users.add(user);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static String searchFacebook(Context context,
			ArrayList<CocoikoUser> users) {
		String url = CocoikoConstains.BASE_URL + "search/facebook";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("users");
					for (int i = 0; i < array.length(); i++) {
						CocoikoUser user = new CocoikoUser();
						JSONObject object = array.getJSONObject(i);
						user.id = object.getInt("id");
						user.nickname = object.getString("nickname");
						user.is_followed = object.getBoolean("is_followed");

						users.add(user);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static String searchTwitter(Context context,
			ArrayList<CocoikoUser> users) {
		String url = CocoikoConstains.BASE_URL + "search/twitter";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("users");
					for (int i = 0; i < array.length(); i++) {
						CocoikoUser user = new CocoikoUser();
						JSONObject object = array.getJSONObject(i);
						user.id = object.getInt("id");
						user.nickname = object.getString("nickname");
						user.is_followed = object.getBoolean("is_followed");

						users.add(user);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/************************************************************************
	 * 
	 * users
	 * 
	 ************************************************************************/

	/**
	 * アイコン取得(GET: http://coco-iko.net/index.php/users/icon/<uid>/<size>)
	 */
	public static String userIcon(Context context, int id, int size) {

		return null;
	}

	/**
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userBlock(Context context, int uid) {
		String url = CocoikoConstains.BASE_URL + "users/block";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userFollow(Context context, int uid) {
		String url = CocoikoConstains.BASE_URL + "users/follow";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * フォロー
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userFollowers(Context context, int uid,
			ArrayList<CocoikoUser> followers) {
		String url = CocoikoConstains.BASE_URL + "users/followers";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("followers");
					for (int i = 0; i < array.length(); i++) {
						JSONObject object = array.getJSONObject(i);

						CocoikoUser user = new CocoikoUser();
						user.id = object.getInt("id");
						user.nickname = object.getString("nickname");
						user.gender = object.getInt("gender");
						user.introduce = object.getString("introduce");
						user.is_followed = object.getBoolean("is_followed");

						followers.add(user);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * フォロー取得
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userFollows(Context context, int uid,
			ArrayList<CocoikoUser> followers) {
		String url = CocoikoConstains.BASE_URL + "users/follows";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("followers");
					for (int i = 0; i < array.length(); i++) {
						JSONObject object = array.getJSONObject(i);

						CocoikoUser user = new CocoikoUser();
						user.id = object.getInt("id");
						user.nickname = object.getString("nickname");
						user.gender = object.getInt("gender");
						user.introduce = object.getString("introduce");
						user.is_followed = object.getBoolean("is_followed");

						followers.add(user);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 自分の情報
	 */
	public static String userMe(Context context, CocoikoAccount account) {
		String url = CocoikoConstains.BASE_URL + "users/me";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {

					JSONObject object = json.getJSONObject("me");
					account.setGender(object.getInt("id"));
					account.setIntroduce(object.getString("self-introduction"));
					account.setFollowing_count(object.getInt("following_count"));
					account.setFollower_count(object.getInt("follower_count"));
					account.setCocoiko_count(object.getInt("kokoiko_count"));
					account.setPoints(object.getInt("points"));
					account.setTwitter_id(object.getInt("twitter_id"));
					account.setFacebook_id(object.getInt("facebook_id"));

					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * ユーザー情報取得
	 * 
	 * @param context
	 * @return
	 */
	public static String userMember(Context context, int uid,
			CocoikoAccount account) {
		String url = CocoikoConstains.BASE_URL + "users/member";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONObject object = json.getJSONObject("member");
					account.setGender(object.getInt("id"));
					account.setIntroduce(object.getString("self-introduction"));
					account.setFollowing_count(object.getInt("following_count"));
					account.setFollower_count(object.getInt("follower_count"));
					account.setCocoiko_count(object.getInt("kokoiko_count"));
					account.setPoints(object.getInt("points"));
					account.setTwitter_id(object.getInt("twitter_id"));
					account.setFacebook_id(object.getInt("facebook_id"));

					JSONObject relation = json.getJSONObject("relation");
					account.setIsfollowed(relation.getBoolean("is_followed"));
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * 自分のポイント確認
	 * 
	 * @param context
	 * @return
	 */
	public static String userPoints(Context context,
			ArrayList<CocoikoPoint> points) {
		String url = CocoikoConstains.BASE_URL + "users/points";

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestGetExecute(url, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					JSONArray array = json.getJSONArray("users");
					for (int i = 0; i < array.length(); i++) {
						CocoikoPoint point = new CocoikoPoint();
						JSONObject object = array.getJSONObject(i);
						point.point = object.getInt("point");
						point.title = object.getString("title");
						point.subtitle = object.getString("subtitle");

						points.add(point);
					}
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}

	}

	/**
	 * Twitter 情報の更新
	 * 
	 * @param context
	 * @param twitter_id
	 * @param twitter_token
	 * @return
	 */
	public static String userTwitter(Context context, int twitter_id,
			String twitter_token) {
		String url = CocoikoConstains.BASE_URL + "users/twitter";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("twitter_id", String
				.valueOf(twitter_id)));
		params.add(new BasicNameValuePair("twitter_token", String
				.valueOf(twitter_token)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * facebook 情報の更新
	 * 
	 * @param context
	 * @param twitter_id
	 * @param twitter_token
	 * @return
	 */
	public static String userFacebook(Context context, int facebook_id,
			String facebook_token) {
		String url = CocoikoConstains.BASE_URL + "users/facebook";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("twitter_id", String
				.valueOf(facebook_id)));
		params.add(new BasicNameValuePair("twitter_token", String
				.valueOf(facebook_token)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * アンブロック
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userUnblock(Context context, int uid) {
		String url = CocoikoConstains.BASE_URL + "users/unblock";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}

	/**
	 * アンフォロー
	 * 
	 * @param context
	 * @param uid
	 * @return
	 */
	public static String userUnfollow(Context context, int uid) {
		String url = CocoikoConstains.BASE_URL + "users/unfollow";

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("uid", String.valueOf(uid)));

		String token = CocoikoAccountStore.restoreToken(context);
		String data = requestPostExecute(url, params, token);
		Log.i("Message", data);

		if (data == null) {
			return "Error";
		} else {
			try {
				JSONObject json = new JSONObject(data);
				boolean status = json.getBoolean("status");
				if (status) {
					return null;
				} else {
					return json.getString("message");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "Error";
			}
		}
	}
}
